local tuilang = fk.CreateSkill {
  name = "lingling__tuilang",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["lingling__tuilang"] = "推浪",
  [":lingling__tuilang"] = "锁定技，当你失去过的总黑色牌数变为三的倍数时，你摸两张牌，弃置场上一张牌。",

  ["@lingling__tuilang"] = "推浪",
  ["#lingling__tuilang-choose"] = "推浪：请弃置场上一张牌！",

}

tuilang:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(tuilang.name) then
      for _, move in ipairs(data) do
        if move.extra_data and move.extra_data.lingling__tuilang and table.contains(move.extra_data.lingling__tuilang, player.id) then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, tuilang.name)
    local targets = table.filter(room.alive_players, function(p)
      return #p:getCardIds("ej") > 0
    end)
    if not player.dead and not player:isNude() and #targets > 0 then
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        skill_name = tuilang.name,
        targets = targets,
        cancelable = false,
        prompt = "#lingling__tuilang-choose"
      })
      if #to > 0 then
        local cid = room:askToChooseCard(player, { target = to[1], flag = "ej", skill_name = tuilang.name })
        room:throwCard(cid, self.name, to[1], player)
      end
    end
  end,

  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(tuilang.name, true) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).color == Card.Black and
                (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player and player:hasSkill(tuilang.name, true) then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).color == Card.Black and
              (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
            room:addPlayerMark(player, "@lingling__tuilang", 1)
          end
        end
        while player:getMark("@lingling__tuilang") >= 3 do
          room:removePlayerMark(player, "@lingling__tuilang", 3)
          if player:getMark("@lingling__tuilang") == 0 then
            move.extra_data = move.extra_data or {}
            move.extra_data.lingling__tuilang = move.extra_data.lingling__tuilang or {}
            table.insertIfNeed(move.extra_data.lingling__tuilang, player.id)
          end
        end
      end
    end
  end,
})

tuilang:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@lingling__tuilang", 0)
end)

return tuilang
